package com.AJava1710D.IO;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 作者: LCG
 * 日期: 2020/7/1 09:57
 * 描述:
 */
public class IORunnerServer implements Runnable {

    private ServerSocket serverSocket;

    public IORunnerServer(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    @Override
    public void run() {

        try {
            while (true){
                //等待接收信息
                Socket accept = serverSocket.accept();

                //处理信息
                InputStream inputStream = accept.getInputStream();

                InputStreamReader inputStreamReader=new InputStreamReader(inputStream);

                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);

                String readLine = bufferedReader.readLine();

                System.out.println("=====server===接收Data==>"+readLine);

                //给客户端响应数据

                String substring = readLine.substring(0, 1);

                OutputStream outputStream = accept.getOutputStream();

                PrintWriter printWriter=new PrintWriter(outputStream);

                printWriter.println(Thread.currentThread().getName()+"====server===sent to client==="+Thread.currentThread().getName()+"==client=="+substring);

                printWriter.flush();

                printWriter.close();
                outputStream.close();
                bufferedReader.close();
                inputStreamReader.close();
                inputStream.close();

                //Thread.sleep(3000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
